
import { ADD_IN_CART, ADD_COUNTER } from './mutation-type.js'
const REMOVE = 'remove_in_cart'
function saveCart(data) {
  localStorage.setItem('car', JSON.stringify(data))
}
export default {
  addTocar({ commit, state }, payload) {
    return new Promise(resolve => {
      let item = state.carList.find(v => v.iid === payload.iid);
      if (item) {
        commit(ADD_COUNTER, item);
        resolve('商品加1')
      } else {
        commit(ADD_IN_CART, payload);
        resolve('添加商品成功')
      }
      saveCart(state.carList)
    })
  },
  removeCar({ commit, state }, payload) {
    return new Promise(resolve => {
      commit(REMOVE, payload);
      resolve('删除成功');
      saveCart(state.carList)
    })
  }
}

